<?php

namespace app\index\controller;

use app\admin\model\Analyst;
use app\admin\model\Analystinfo;
use app\admin\model\Result;
use app\admin\model\Schedule;
use app\common\controller\Base;
use think\Config;
use think\Cookie;
use think\Hook;
use think\Request;
use think\Session;
use think\Validate;

/**
 * 会员中心
 */
class User extends Base
{

    public function _initialize()
    {
        parent::_initialize();
    }


    /**
     * 会员中心
     */
    public function index()
    {
        return $this->view->fetch();
    }

    /**
     * 会员
     *
     * @return string
     * @throws \think\Exception
     */
    public function vip()
    {
        $id = Session('user.id');
        $user = \app\admin\model\User::get($id);
        return $this->view->fetch('vip', ['user' => $user]);
    }

    /**
     * 修改資料
     *
     * @param Request $request
     * @return string
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function modify(Request $request)
    {
        $id = Session('user.id');
        if ($request->isPost()) {
            $nickname = $this->request->post('nickname');
            $email = $this->request->post('email');
            $qq = $this->request->post('qq', '');
            $wx = $this->request->post('wx', '');
            $mobile = $this->request->post('mobile');
            $token = $this->request->post('__token__');
            $rule = [
                'nickname'  => 'require|length:3,30',
                'email'     => 'require|email',
                'qq'     => 'integer',
                'wx'     => 'alphaDash',
                'mobile'    => 'require|regex:/^1\d{10}$/',
                '__token__' => 'token',
            ];

            $msg = [
                'nickname.require' => '暱稱不能為空',
                'nickname.length'  => '暱稱在3-30個字之間',
                'email'            => 'Email錯誤',
                'qq'            => 'QQ號碼錯誤',
                'wx'            => '微信號碼錯誤',
                'mobile'           => '手機號碼錯誤',
            ];
            $data = [
                'nickname'  => $nickname,
                'email'     => $email,
                'mobile'    => $mobile,
                'qq'    => $qq,
                'wx'    => $wx,
                // 'captcha'   => $captcha,
                '__token__' => $token,
            ];
            $validate = new Validate($rule, $msg);
            $result = $validate->check($data);
            if (!$result) {
                $this->error($validate->getError());
            }

            $user = \app\admin\model\User::where(['id' => $id])->update([
                'nickname' => $nickname,
                'email' => $email,
                'mobile' => $mobile,
                'qq' => $qq,
                'wx' => $wx
            ]);
            if ($user) {
                $this->success('修改成功', url('index/user/vip'));
            } else {
                $this->error('沒有變化，請重新修改');
            }
        }
        $user = \app\admin\model\User::get($id);
        return $this->view->fetch('vip2', ['user' => $user]);
    }

    /**
     * 免费单
     *
     * @return string
     * @throws \think\Exception
     */
    public function free()
    {
        $list = Schedule::where(['category' => 1])->limit(12)->order('id', 'desc')->select();
        $result = Result::limit(4)->order('id', 'desc')->select();
        return $this->view->fetch('', [
            'left' => $list,
            'right' => $result
        ]);
    }

    /**
     * 全部比賽結果
     *
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function results()
    {
        $list = Result::order('id', 'desc')->paginate(15);
        return $this->view->fetch('result', [
            'list' => $list
        ]);
    }


    /**
     * 职业单
     *
     * @return string
     * @throws \think\Exception
     */
    public function profession()
    {
        $level = \session('user.level');
        if ($level < 2) {
            $this->error('請升級VIP查看職業單', url('index/index/payment', ['id' => 0]));
        }
        $list = Schedule::where(['category' => 2])->limit(12)->order('id', 'desc')->select();
        $result = Result::limit(4)->order('id', 'desc')->select();
        return $this->view->fetch('', [
            'left' => $list,
            'right' => $result
        ]);
    }

    /**
     * 分析师
     *
     * @param Request $request
     * @return string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function widget(Request $request)
    {
        $id = $request->get('id', 3);
        $title1 = Analyst::where(['country' => $id, 'title' => 1])->order('id', 'desc')->find();
        $title2 = Analyst::where(['country' => $id, 'title' => 2])->order('id', 'desc')->find();

        $list1 = [];
        $list2 = [];
        if (!empty($title1)) {
            $list1 = Analystinfo::where(['analyst_id' => $title1->id])->limit(5)->order('id', 'desc')->select();
        }
        if (!empty($title2)) {
            $list2 = Analystinfo::where(['analyst_id' => $title2->id])->limit(5)->order('id', 'desc')->select();
        }

        return $this->view->fetch('', [
            'id' => $id,
            'title1' => $title1,
            'title2' => $title2,
            'list1' => $list1,
            'list2' => $list2,
        ]);
    }

    /**
     * 修改密码
     */
    public function changepwd()
    {
        if ($this->request->isPost()) {
            $oldpassword = $this->request->post("oldpassword");
            $newpassword = $this->request->post("newpassword");
            $renewpassword = $this->request->post("renewpassword");
            $token = $this->request->post('__token__');
            $rule = [
                'oldpassword'   => 'require|length:6,30',
                'newpassword'   => 'require|length:6,30',
                'renewpassword' => 'require|length:6,30|confirm:newpassword',
                '__token__'     => 'token',
            ];

            $msg = [
            ];
            $data = [
                'oldpassword'   => $oldpassword,
                'newpassword'   => $newpassword,
                'renewpassword' => $renewpassword,
                '__token__'     => $token,
            ];
            $field = [
                'oldpassword'   => __('Old password'),
                'newpassword'   => __('New password'),
                'renewpassword' => __('Renew password')
            ];
            $validate = new Validate($rule, $msg, $field);
            $result = $validate->check($data);
            if (!$result) {
                $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
                return FALSE;
            }

            $ret = $this->auth->changepwd($newpassword, $oldpassword);
            if ($ret) {
                $synchtml = '';
                ////////////////同步到Ucenter////////////////
                if (defined('UC_STATUS') && UC_STATUS) {
                    $uc = new \addons\ucenter\library\client\Client();
                    $synchtml = $uc->uc_user_synlogout();
                }
                $this->success(__('Reset password successful') . $synchtml, url('user/login'));
            } else {
                $this->error($this->auth->getError(), null, ['token' => $this->request->token()]);
            }
        }
        $this->view->assign('title', __('Change password'));
        return $this->view->fetch();
    }

}
